/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

//===========================================================================================
#include <dylab/utils/FileName.h>
#include <dylab/utils/exceptions/EAssertionFailed.h>
#include <dylab/utils/exceptions/EDoesNotExists.h>

//===========================================================================================
dylab::FileName::FileName(const String & _fileName)
: mFileName(_fileName)
{ 
    DYLAB_ASSERT(!mFileName.empty()); 
}
//-----------------------------------------------------------------------------
dylab::String dylab::FileName::getPath() const
{
    // utility path variable
    String path;

    // find the last position of the slash
    size_t slashPos = mFileName.rfind("/");
    if (slashPos != String::npos) // get String before the slash
        path = mFileName.substr(0, slashPos + 1);
    else // the file name has no path
        path = "";

    // return the found path (or empty String)
    return path;
}
//-----------------------------------------------------------------------------
dylab::String dylab::FileName::getName() const
{
    // utility name variable
    String name = mFileName;
    
    if (name.contains("/"))
        name = name.getLastRightPart("/");
    else if (name.contains("\\"))
        name = name.getLastRightPart("\\");

    name = name.getFirstLeftPart(".");

    // return the found name
    return name;
}
//-----------------------------------------------------------------------------
bool dylab::FileName::hasExtension() const
{ 
    // file name has extension if it contains at least one "dot" character
    return mFileName.find(".") != String::npos;
}
//-----------------------------------------------------------------------------
dylab::String dylab::FileName::getLastExtension() const
{ 
    // find last extension of the file name
    size_t dotPos = mFileName.rfind(".");
    if (dotPos == String::npos)
        throw EDoesNotExists(String::fmt("The specified file name has no extension: '%1%'", mFileName));    

    // return the found last extension
    return mFileName.substr(dotPos + 1);
}
//-----------------------------------------------------------------------------